﻿// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

namespace Microsoft.Samples.WindowsPhoneCloud.Phone.Pages
{
    using System;
    using System.Collections.Generic;
    using System.IO.IsolatedStorage;
    using System.Windows;
    using System.Windows.Navigation;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    using Microsoft.Samples.WindowsPhoneCloud.Phone.Helpers;
    using Microsoft.Samples.WindowsPhoneCloud.Phone.ViewModel;
    using SL.Phone.Federation.Utilities;

    public partial class LoginPage : PhoneApplicationPage
    {
        private readonly RequestSecurityTokenResponseStore rstrStore = App.Current.Resources["rstrStore"] as RequestSecurityTokenResponseStore;

        public LoginPage()
        {
            this.InitializeComponent();

            this.PageTransitionReset.Begin();
            this.SignInControl.RequestSecurityTokenResponseCompleted +=
                (s, e) => this.ViewModel.HandleRequestSecurityTokenResponseCompleted(
                    s,
                    e,
                    () =>
                    {
                        SetIsolatedStorageSetting("UserIsRegistered", true);
                        this.NavigationService.Navigate(new Uri("/Pages/MainPage.xaml", UriKind.Relative));
                    },
                    () =>
                    {
                        SetIsolatedStorageSetting("UserIsRegistered", false);
                        this.NavigationService.Navigate(new Uri("/Pages/RegisterPage.xaml", UriKind.Relative));
                    },
                    errorMessage =>
                    {
                        this.SignInControl.GetSecurityToken();

                        errorMessage = string.IsNullOrEmpty(errorMessage)
                            ? "An error occurred determining if the user was already registered. Make sure that the appropriate SSL certificate is installed."
                            : errorMessage;
                        MessageBox.Show(errorMessage, "Registration Error", MessageBoxButton.OK);
                    });
            this.SignInControl.NavigatingToIdentityProvider +=
                (s, e) =>
                {
                    // When the user navigates to an identity provider, the previous login information is cleaned up.
                    SetIsolatedStorageSetting("UserIsRegistered", false);
                    App.CloudClientFactory.CleanAuthenticationToken();
                };
        }

        public LoginPageViewModel ViewModel
        {
            get { return this.DataContext as LoginPageViewModel; }
            set { this.DataContext = value; }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (this.rstrStore.IsValid() && GetIsolatedStorageSetting<bool>("UserIsRegistered") && !GetApplicationState<bool>("UserBackPress"))
            {
                this.NavigationService.Navigate(new Uri("/Pages/MainPage.xaml", UriKind.Relative));
            }
            else
            {
                this.StartTransition();

                SetApplicationState("UserBackPress", false);

                this.SignInControl.GetSecurityToken();
            }
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            this.PageTransitionReset.Begin();
        }

        private static T GetApplicationState<T>(string key)
        {
            if (!PhoneApplicationService.Current.State.ContainsKey(key))
            {
                return default(T);
            }

            return (T)PhoneApplicationService.Current.State[key];
        }

        private static void SetApplicationState(string key, object value)
        {
            if (PhoneApplicationService.Current.State.ContainsKey(key))
            {
                PhoneApplicationService.Current.State.Remove(key);
            }

            PhoneApplicationService.Current.State.Add(key, value);
        }

        private static T GetIsolatedStorageSetting<T>(string key)
        {
            IDictionary<string, object> isolatedStorage = IsolatedStorageSettings.ApplicationSettings;
            if (!isolatedStorage.ContainsKey(key))
            {
                return default(T);
            }

            return (T)isolatedStorage[key];
        }

        private static void SetIsolatedStorageSetting(string key, object value)
        {
            IDictionary<string, object> isolatedStorage = IsolatedStorageSettings.ApplicationSettings;
            if (isolatedStorage.ContainsKey(key))
            {
                isolatedStorage.Remove(key);
            }

            isolatedStorage.Add(key, value);
        }

        private void StartTransition()
        {
            this.PageTransitionIn.Begin();
            if (this.ViewModel == null)
            {
                this.ViewModel = new LoginPageViewModel();
            }
        }
    }
}